<template>
	<view>
		<view style="width: 100%;height: 20rpx;background-color: #F5F7F9;" v-if="list.length>0"></view>
		<!-- 提现记录列表 -->
		<view class="record-list" v-if="list.length>0">
			<view class="record-item" v-for="item in list" :key="item.id">
				<view class="record-header">
					<view class="record-title">
						{{item.text}}
					</view>
					<view class="record-amount">
						¥{{item.money}}
					</view>
				</view>
				<view class="record-content">
					<view class="record-time">
						{{item.create_time}}
					</view>
					<view class="record-status" :class="'status-' + getStatusType(item.is_status)">
						{{getStatusText(item.is_status)}}
					</view>
				</view>
				<view class="record-note" v-if="item.note && item.note !== ''">
					<text class="note-label">备注：</text>
					<text class="note-content">{{item.note}}</text>
				</view>
				<!-- 确认收款按钮 - 仅对已通过审核的记录显示 -->
				<view class="confirm-section" v-if="item.is_status == 1 && item.is_type==0 && item.confirm==0">
					<button 
						class="confirm-btn" 
						:disabled="item.confirm==1"
						@click="confirmReceipt(item)"
					>
						{{ item.confirm==1 ? '已确认收款' : '确认收款'}}
					</button>
				</view>
				<view v-if="item.confirm==1" :style="{fontSize: '8px', color: '#aaaaaa',textAlign: 'right',display: 'flex' ,justifyContent: 'flex-end'}">已确认收款</view>
			</view>
		</view>
		<!-- 空状态 -->
		<view class="empty-state" v-else>
			<view class="empty-icon">📋</view>
			<view class="empty-text">暂无提现记录</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				user_id:-1
			}
		},
		onLoad() {
			
		},
		mounted(){
			// 从lifeData里获取user_id
			this.user_id = uni.getStorageSync('lifeData').vuex_user.user_id
		},
		activated () {
			console.log('activated ')
			this.getRecordList()
		},
		methods:{
			// 获取提现记录
			getRecordList() {
				this.$u.api.shop.getRecord().then(res => {
					console.log('提现记录:', res);
					if(res.code == 200){
						this.list = res.data
					}
				}).catch(err => {
					console.error('获取提现记录失败:', err);
					this.$u.toast('获取记录失败')
				})
			},
			// 确认收款
			confirmReceipt(item) {
				let that = this;
				uni.showModal({
					title: '确认收款',
					content: `确认已收到提现金额 ¥${item.money} 吗？`,
					success: (res) => {
						if (res.confirm) {
							this.$u.api.shop.confirmReceipt({
								id: item.id,
								user_id: that.user_id
							}).then(res => {
								console.log("确认收款结果：",res.data)
								if(res.code == 1){
									// 更新本地状态

									that.callJsapiConfirm(res.data,item.id)

									// 重新获取列表
									this.getRecordList()
								} else {
									this.$u.toast(res.msg || '确认收款失败')
								}
							}).catch(err => {
								console.error('确认收款失败:', err);
								this.$u.toast('确认收款失败')
							})
						}
					}
				})
			},
			// 调起jsapi确认收款
			callJsapiConfirm(item,id) {
				let that = this
				console.log("调起jsapi确认收款：",item)
				uni.requestMerchantTransfer({
					mchId: item.mchId || '1699363951',
					appId: item.appId || 'wxbd120128305f51fc',
					package: item.package || '',
					success: (res) => {
						console.log('jsapi转账确认成功:', res);
						this.$u.toast('确认收款成功')
						
						// 调用confirm_withdraw接口
						this.$u.api.shop.confirm_withdraw({
							id: id
						}).then(res => {
							console.log('confirm_withdraw接口调用结果:', res);
							if(res.code == 200 || res.code == 1){
								console.log('confirm_withdraw接口调用成功');
								// 找到对应的列表项并更新状态
								let targetItem = that.list.find(item => item.id == id);
								if(targetItem) {
									that.$set(targetItem, 'confirm', 1);
								}
								
							} else {
								console.error('confirm_withdraw接口调用失败:', res.msg);
							}
							
						}).catch(err => {
							console.error('confirm_withdraw接口调用异常:', err);
						}).finally(()=>{
							// 重新获取列表
							that.getRecordList()
						})

					},
					fail: (res) => {
						this.$u.api.shop.cancelConfirm({id:id}).then(res => {
							console.log('取消提现:', res);
						}).catch(err => {
							console.error('取消提现失败:', err);
						})
						that.getRecordList()
					},
					complete: (res) => {
						console.log('jsapi转账确认完成:', res);
					}
				})
			},
			// 获取状态文本
			getStatusText(status) {
				const statusMap = {
					0: '待审核',
					1: '已通过',
					2: '已拒绝'
				}
				return statusMap[status] || '未知状态'
			},
			// 获取状态类型
			getStatusType(status) {
				const typeMap = {
					0: 'pending',
					1: 'success',
					2: 'rejected'
				}
				return typeMap[status] || 'unknown'
			}
		}
	}
</script>

<style>
	page{
		background-color: #F5F7F9;
	}
	
	/* 记录列表 */
	.record-list {
		padding: 0 30rpx;
	}
	
	/* 记录项 */
	.record-item {
		background-color: white;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
	}
	
	/* 记录头部 */
	.record-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}
	
	.record-title {
		font-size: 32rpx;
		color: #333333;
		font-weight: 500;
	}
	
	.record-amount {
		font-size: 36rpx;
		color: #FF6B35;
		font-weight: bold;
	}
	
	/* 记录内容 */
	.record-content {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 15rpx;
	}
	
	.record-time {
		font-size: 28rpx;
		color: #999999;
	}
	
	.record-status {
		font-size: 28rpx;
		padding: 8rpx 16rpx;
		border-radius: 20rpx;
	}
	
	.status-pending {
		background-color: #FFF7E6;
		color: #FFA500;
	}
	
	.status-success {
		background-color: #E8F5E8;
		color: #52C41A;
	}
	
	.status-rejected {
		background-color: #FFF2F0;
		color: #FF4D4F;
	}
	
	/* 备注 */
	.record-note {
		padding-top: 15rpx;
		border-top: 1rpx solid #F0F0F0;
	}
	
	.note-label {
		font-size: 26rpx;
		color: #666666;
	}
	
	.note-content {
		font-size: 26rpx;
		color: #333333;
	}
	
	/* 确认收款区域 */
	.confirm-section {
		margin-top: 20rpx;
		padding-top: 20rpx;
		border-top: 1rpx solid #F0F0F0;
	}
	
	.confirm-btn {
		width: 100%;
		height: 80rpx;
		background: linear-gradient(135deg, #FF6B35, #FF8C42);
		color: white;
		border: none;
		border-radius: 40rpx;
		font-size: 30rpx;
		font-weight: 500;
		display: flex;
		align-items: center;
		justify-content: center;
		transition: all 0.3s ease;
	}
	
	.confirm-btn:disabled {
		background: #E0E0E0;
		color: #999999;
	}
	
	.confirm-btn:not(:disabled):active {
		transform: scale(0.98);
		opacity: 0.9;
	}
	
	/* 空状态 */
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;
	}
	
	.empty-icon {
		font-size: 80rpx;
		margin-bottom: 20rpx;
	}
	
	.empty-text {
		font-size: 28rpx;
		color: #999999;
	}
</style>